using UnityEditor;
using UnityEngine;

public class CheckShaders
{
    [MenuItem("HRP/G-Game/检查非HRP的Shader")]
    public static void Do()
    {
        //var allLoadedMaterials = Resources.FindObjectsOfTypeAll<Material>();
        //var allLoadedMaterials = Object.FindObjectsOfType<Material>();

        var renderers = Object.FindObjectsOfType<Renderer>();
        //var materials = new List<Material>();
        foreach (var renderer in renderers)
        {
            var materials = renderer.sharedMaterials;
            if (renderer is ParticleSystemRenderer particleSystemRenderer)
            {
                // ParticleSystemRenderer 包含 Material 和 TrailMaterial，后者可以为空
                if (particleSystemRenderer.trailMaterial == null)
                {
                    materials = new[]
                    {
                        particleSystemRenderer.sharedMaterial
                    };
                }
            }

            foreach (var material in materials)
            {
                if (material == null)
                {
                    // 材质丢失不是本功能关心的重点
                    Debug.LogWarning("材质丢失: " + renderer.name, renderer);
                    continue;
                }

                var shader = material.shader;

                if (shader.name.Equals("Hidden/InternalErrorShader"))
                {
                    Debug.LogError($"着色器丢失: {renderer.name} - {material.name}", renderer);
                    continue;
                }

                string shaderPath = AssetDatabase.GetAssetPath(shader);

                if (shaderPath == "Library/unity editor resources")
                {
                    continue;
                }

                if (shaderPath == "Resources/unity_builtin_extra")
                {
                    Debug.LogError($"使用了内置着色器: {renderer.name} - {material.name} - {shader.name}", renderer);
                    continue;
                }

                if (shaderPath.StartsWith("Packages/com.yoozoo.owl.rendering.hrp/"))
                {
                    continue;
                }

                if (shaderPath.StartsWith("Assets/HRP/HRPshader"))
                {
                    continue;
                }

                if (shaderPath.StartsWith("Assets/TextMesh Pro"))
                {
                    // TextMesh Pro显示正常
                    continue;
                }

                Debug.LogError($"没有使用HRP着色器: {renderer.name} - {material.name} - {shader.name}\n{shaderPath}", renderer);
            }
        }
    }
        
}
